package kyssion.leetcode.num101_150;

import kyssion.leetcode.util.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class code113_路径总和2 {

    /**
     * 5
     * / \
     * 4   8
     * /   / \
     * 11  13  4
     * /  \    / \
     * 7    2  5   1
     *
     * @param args
     */
    public static void main(String[] args) {

        TreeNode treeNode = TreeNode.create(
                new int[]{
                }
        );

        List<List<Integer>> listList = new code113_路径总和2().pathSum(treeNode, 22);
        System.out.println();
    }

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> listList = new ArrayList<>();
        if (root != null) {
            ans(root, sum, 0, listList, new ArrayList<>());
        }
        return listList;
    }

    private void ans(TreeNode root, int sum, int all, List<List<Integer>> listList, ArrayList<Integer> list) {
        if (root == null) {
            return;
        } else if (root.left == null && root.right == null) {
            if (all + root.val == sum) {
                list.add(root.val);
                listList.add(new ArrayList<>(list));
                list.remove(list.size() - 1);
            }
        } else {
            all += root.val;
            list.add(root.val);
            ans(root.left, sum, all, listList, list);
            ans(root.right, sum, all, listList, list);
            list.remove(list.size() - 1);
        }
    }
}
